/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.kimstore.gui.staff;

import com.kimstore.bean.Staff;
import com.kimstore.factory.DaoFactory;
import com.kimstore.main.controller.StaffTabController;

/**
 *
 * @author Administrator
 */
public class NewStaffController {
    
    private StaffTabController parentController = null;
    private NewStaffWindow window = null;
    
    public NewStaffController(StaffTabController parentController){
        this.parentController = parentController;
    }
    
    public void displayWindow(){
        window = new NewStaffWindow(this);
        window.showWindow();
    }
    
    public void fromWindow(Staff staff){
        DaoFactory.getInstance().getStaffDao().saveOrUpdate(staff);
        
        parentController.createNewStaffControllerCallback(staff);
    }
    
    public void validate(Staff staff) throws Error {
        if(staff.getName() == null || staff.getName().isEmpty()) {
            throw new Error("<html><b>Staff name</b>  has not been filled up.</html>");
        }
        
        boolean exists = DaoFactory.getInstance().getStaffDao().existsName(staff.getName());
        
        if(exists) {
            throw new Error("<html><b>Staff name</b> has already been taken.</html>");
        }
        
        
    }

    public boolean isUsernameTaken(String text) {
        return DaoFactory.getInstance().getUserDao().existsUsername(text);
    }
    
}
